package com.nbjtjc.safe.service.safe;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.common.bean.INSPECTTYPEENUM;
import com.nbjtjc.common.bean.WEEKENDENUM;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.DangerInspectPublicMapper;
import com.nbjtjc.safe.mapper.safe.SafeDangerInspectPlanMapper;

import com.nbjtjc.safe.mapper.safe.SafeEnterprisePlanMapper;
import com.nbjtjc.safe.mapper.safe.SafeUserPlanMapper;
import com.nbjtjc.safe.model.danger.DangerInspectPublic;
import com.nbjtjc.safe.model.safe.SafeDangerInspectPlan;
import com.nbjtjc.safe.model.safe.SafeDangerInspectTask;
import com.nbjtjc.safe.model.safe.SafeEnterprisePlan;
import com.nbjtjc.safe.model.safe.SafeUserPlan;
import com.nbjtjc.safe.util.FREQUENCYENUM;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SafeDangerInspectPlanServiceImpl {
    @Autowired
    private SafeDangerInspectPlanMapper safeDangerInspectPlanMapper;

    @Autowired
    private SafeInspectTablePublicServiceImpl safeInspectTablePublicService;

    @Autowired
    private SafeDangerInspectTaskServiceImpl taskService;
    @Autowired
    private SafeUserPlanServiceImpl safeUserPlanService;

    @Autowired
    private SafeEnterprisePlanServiceImpl safeEnterprisePlanService;
    @Autowired
    private DangerInspectPublicMapper inspectPublicMapper;

    @Autowired
    private SafeUserPlanMapper safeUserPlanMapper;

    @Autowired
    private SafeEnterprisePlanMapper safeEnterprisePlanMapper;

    public Result search(SafeDangerInspectPlan safeDangerInspectPlan) {
        Result result = new Result();
        PageHelper.startPage(safeDangerInspectPlan.getPageCurrent()
                , safeDangerInspectPlan.getPageSize());
        if (StringUtils.isEmpty(safeDangerInspectPlan.getName())) {
            safeDangerInspectPlan.setName(null);
        }
        List<SafeDangerInspectPlan> safeDangerInspectPlanList = safeDangerInspectPlanMapper.search(safeDangerInspectPlan);
        safeDangerInspectPlanList.forEach(plan -> {
            plan.setFrequencyStr(FREQUENCYENUM.getName(plan.getFrequency()));
            plan.setWeekendStr(WEEKENDENUM.getName(plan.getWeekend()));
            plan.setInspectTypeStr(INSPECTTYPEENUM.getName(plan.getInspectType()));
        });
        result.setData(new PageInfo<>(safeDangerInspectPlanList));
        return result;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public Result add(SafeDangerInspectPlan safeDangerInspectPlan) {
        Result result = new Result();
        safeDangerInspectPlan.setCreateUserId(MyShiroUtils.getSessionUserId());
        safeDangerInspectPlan.setCreateUser(MyShiroUtils.getSessionUserName());
        safeDangerInspectPlan.setDeptId(MyShiroUtils.getDeptId());
        safeDangerInspectPlan.setCreateTime(new Date());
        safeDangerInspectPlan.setCompanyId(MyShiroUtils.getCompanyId());
        safeDangerInspectPlanMapper.insertSelective(safeDangerInspectPlan);
        safeInspectTablePublicService.publicTables(safeDangerInspectPlan.getDangerInspectPublicList()
                , safeDangerInspectPlan, MyShiroUtils.getSessionUserId(), true);
        safeUserPlanService.addBatch(safeDangerInspectPlan);//插入检查人员
        safeEnterprisePlanService.addBatch(safeDangerInspectPlan);//插入检查企业
        //todo 生成task
        taskService.generateEnterpriseTaskByPlan(safeDangerInspectPlan.getId(), MyShiroUtils.getSessionUserId());
        result.setData(safeDangerInspectPlan);
        return result;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public Result addWithTaskPlan(SafeDangerInspectPlan safeDangerInspectPlan) {
        Result result = new Result();
        safeDangerInspectPlan.setCreateUserId(MyShiroUtils.getSessionUserId());
        safeDangerInspectPlan.setCreateUser(MyShiroUtils.getSessionUserName());
        safeDangerInspectPlan.setDeptId(MyShiroUtils.getDeptId());
        safeDangerInspectPlan.setCreateTime(new Date());
        safeDangerInspectPlan.setCompanyId(MyShiroUtils.getCompanyId());
        safeDangerInspectPlanMapper.insertSelective(safeDangerInspectPlan);
        safeDangerInspectPlan.getDangerInspectPublicList().forEach(dangerInspectPublic -> {
            dangerInspectPublic.setInspectId(dangerInspectPublic.getId());//前端穿参数的时候inspectId放到类id上去，所以放回去
        });
        safeInspectTablePublicService.publicTables(safeDangerInspectPlan.getDangerInspectPublicList()
                , safeDangerInspectPlan, MyShiroUtils.getSessionUserId(), true);
        //todo 生成task
//        taskService.generateTaskByPlan(safeDangerInspectPlan.getId(),MyShiroUtils.getSessionUserId());
        List<SafeDangerInspectTask> taskPlanList = taskService.gengerateTaskPlan(safeDangerInspectPlan.getId(), MyShiroUtils.getSessionUserId(), MyShiroUtils.getSessionUserName()
                , safeDangerInspectPlan.getDangerInspectTaskList());
        taskService.publicTaskInDays(taskPlanList, 3, safeDangerInspectPlan.getId());
        result.setData(safeDangerInspectPlan);
        return result;
    }


    @Transactional
    public Result update(SafeDangerInspectPlan safeDangerInspectPlan) {
        Result result = new Result();
        safeDangerInspectPlanMapper.updateByPrimaryKeySelective(safeDangerInspectPlan);
        //更新计划的检查表
        DangerInspectPublic inspectPublic = new DangerInspectPublic();
        inspectPublic.setInspectPlanId(safeDangerInspectPlan.getId());
        inspectPublicMapper.delete(inspectPublic);

        safeDangerInspectPlan.getDangerInspectPublicList().forEach(dangerInspectPublic -> {
            dangerInspectPublic.setInspectId(dangerInspectPublic.getId());//前端穿参数的时候inspectId放到类id上去，所以放回去
        });
        safeInspectTablePublicService.publicTables(safeDangerInspectPlan.getDangerInspectPublicList()
                , safeDangerInspectPlan, MyShiroUtils.getSessionUserId(), true);
        //更新检查计划的检查企业
        safeEnterprisePlanService.updateEnterprisePlan(safeDangerInspectPlan.getSafeEnterprisePlanList()
                , safeDangerInspectPlan);
        //更细检查计划的检查人员
        safeUserPlanService.updateSafeUserPlan(
                safeDangerInspectPlan.getSafeUserPlanList(), safeDangerInspectPlan);
        result.setData(safeDangerInspectPlan);
        return result;
    }

    public Result delete(SafeDangerInspectPlan safeDangerInspectPlan) {
        Result result = new Result();
        safeDangerInspectPlanMapper.deleteByPrimaryKey(safeDangerInspectPlan.getId());
        taskService.deleteTaskByPlanId(safeDangerInspectPlan.getId());
        result.setData(safeDangerInspectPlan);
        return result;
    }


    public Result info(SafeDangerInspectPlan safeDangerInspectPlan) {
        Result result = new Result();
        safeDangerInspectPlan = safeDangerInspectPlanMapper.selectByPrimaryKey(safeDangerInspectPlan.getId());
        if (ObjectUtil.isNull(safeDangerInspectPlan)) {
            return new Result("没有该计划", false);
        }
        List<SafeUserPlan> safeUserPlans = safeUserPlanMapper.findByPlanId(safeDangerInspectPlan.getId());
        safeDangerInspectPlan.setSafeUserPlanList(safeUserPlans);
        List<SafeEnterprisePlan> safeEnterprisePlanList =
                safeEnterprisePlanMapper.findByPlanId(safeDangerInspectPlan.getId());
        safeDangerInspectPlan.setSafeEnterprisePlanList(safeEnterprisePlanList);
        result.setData(safeDangerInspectPlan);
        return result;
    }
}
